﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NIIT.SaoBacDau.Aupi.HospitalManagement.Entities;
using NIIT.SaoBacDau.Aupi.HospitalManagement.Languages;
using NIIT.SaoBacDau.Aupi.HospitalManagement.BusinessLogic;

namespace NIIT.SaoBacDau.Aupi.HospitalManagement
{

    public partial class Managements : Form
    {
        public Boolean bln_RememberPassword { get; set; }

        private HR_Login log;

        private HR_Employee hre;

        private int Hours, Minutes, Second; string bank1, bank2, bank3;

        #region Phần form quản lý bệnh viện

        public Managements()
        {
            InitializeComponent();
        }

        public Managements(HR_Login objlog, HR_Employee hre)
        {
            this.log = objlog;
            this.hre = hre;
            InitializeComponent();
        }

        private void Managements_Load(object sender, EventArgs e)
        {
            Clock();
            DisableManagement();
            if (BL_HR_Login.Account == HM_Account.Login)
            {
                LoginSystems logsystem = new LoginSystems();
                logsystem.ShowDialog();
            }
            ShowName();
            log = BL_HR_Login.objlogin;
            hre = BL_HR_Login.EmployeeName;
            EnableManagement();
        }    

        public void ProfileLoginSystem(HR_Employee objemp,HR_Login objlog ,Boolean RememberPassWord)
        {
            this.log = objlog;
            this.hre = objemp;
            bln_RememberPassword = RememberPassWord;
        }

        #endregion

        #region Phần sự kiện trên form

        private void Managements_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult DR = MessageBox.Show("Bạn muốn đóng ứng dụng hệ thống quản lý bệnh viện ?", "Thoát Hệ Thống", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (DR == DialogResult.Yes)
            {
                e.Cancel = false;
                Application.ExitThread();
            }
            else
                e.Cancel = true;
        }

        private void tsb_AddPatient_Click(object sender, EventArgs e)
        {
            Clear();
            ShowAddPatient(HM_ReceptionAndManagementPatient.Patient_Add);
        }

        private void tsb_EditPatient_Click(object sender, EventArgs e)
        {
            ShowPatient(HM_ReceptionAndManagementPatient.Patient_Edit);
        }

        private void tsb_SearchPatient_Click(object sender, EventArgs e)
        {
            ShowPatient(HM_ReceptionAndManagementPatient.Patient_Search);
        }

        private void tsb_MedicalHistory_Click(object sender, EventArgs e)
        {
            ShowPatientHistory(HM_ReceptionAndManagementPatient.Patient_MedicalHistory);
        }

        private void tsb_PatientPayment_Click(object sender, EventArgs e)
        {
            Clear();
            ShowPatient(HM_ReceptionAndManagementPatient.Patient_Payment);
        }

        private void tsb_ReportRecepPatient_Click(object sender, EventArgs e)
        {

        }

        private void tsb_StatisticPatient_Click(object sender, EventArgs e)
        {

        }

        private void tsb_Papers_Click(object sender, EventArgs e)
        {

        }

        private void tsmi_Acount_Click(object sender, EventArgs e)
        {
            Clear();
            ShowAccount(this.hre);
        }

        private void tsmi_Logout_Click(object sender, EventArgs e)
        {
            if (LibraryLanguages.Logout() == true)
            {
                timer_Clock.Stop();
                this.Hide();
                if (BL_HR_Login.Cookie == HM_Cookie.RememberPassword)
                {
                    LoginSystems logsystem = new LoginSystems(log,HM_Cookie.RememberPassword,HM_Account.Logout);
                    logsystem.ShowDialog();
                }
                else
                {
                    log = null;
                    LoginSystems logsystem = new LoginSystems(log,HM_Cookie.NoRememberPassword,HM_Account.Logout);
                    logsystem.ShowDialog();
                }
            }
        }

        private void tsmi_ProfilePassWord_Click(object sender, EventArgs e)
        {
            Clear();
            ProfilePassword proPass = new ProfilePassword();
            proPass.MdiParent = this;
            proPass.Show();
        }

        private void tsmi_Help_Click(object sender, EventArgs e)
        {

        }

        private void tsmi_SettingSystem_Click(object sender, EventArgs e)
        {

        }

        private void tsmi_Backup_Click(object sender, EventArgs e)
        {

        }

        private void tsmi_Import_Click(object sender, EventArgs e)
        {

        }

        private void tsmi_SettingDatabase_Click(object sender, EventArgs e)
        {

        }

        private void tsmi_Update_Click(object sender, EventArgs e)
        {

        }

        #endregion

        #region Phần phương thức xử lý

        private void ShowName()
        {
            if (BL_HR_Login.EmployeeName == null)
            {
                tssl_Name.Text = "";
                tssl_WellCome.Text = "";
            }
            else
            {
                tssl_WellCome.Text = "Xin Chào";
                tssl_Name.Text = BL_HR_Login.EmployeeName.NameEmployee;
            }
        }

        private void DisableElementReceptionAndManagementPatient()
        {
            tsb_AddPatient.Enabled = false;
            tsb_EditPatient.Enabled = false;
            tsb_SearchPatient.Enabled = false;
            tsb_MedicalHistory.Enabled = false;
            tsb_PatientPayment.Enabled = false;
            tsb_ReportRecepPatient.Enabled = false;
            tsb_StatisticPatient.Enabled = false;
            tsb_Papers.Enabled = false;
        }

        public void DisableControl(HM_ReceptionAndManagementPatient option)
        {
            DisableElementReceptionAndManagementPatient();
            switch (option)
            {
                case HM_ReceptionAndManagementPatient.Patient_Add:
                    tsb_AddPatient.Enabled = true;
                    break;
                case HM_ReceptionAndManagementPatient.Patient_Edit:
                    tsb_EditPatient.Enabled = true;
                    break;
                case HM_ReceptionAndManagementPatient.Patient_Search:
                    tsb_SearchPatient.Enabled = true;
                    break;
                case HM_ReceptionAndManagementPatient.Patient_MedicalHistory:
                    tsb_MedicalHistory.Enabled = true;
                    break;
                case HM_ReceptionAndManagementPatient.Patient_Payment:
                    tsb_PatientPayment.Enabled = true;
                    break;
                case HM_ReceptionAndManagementPatient.Patient_Report:
                    tsb_ReportRecepPatient.Enabled = true;
                    break;
                case HM_ReceptionAndManagementPatient.Patient_Statistic:
                    tsb_StatisticPatient.Enabled = true;
                    break;
                case HM_ReceptionAndManagementPatient.Papers:
                    tsb_Papers.Enabled = true;
                    break;
                default:
                    tsb_AddPatient.Enabled = true;
                    tsb_EditPatient.Enabled = true;
                    tsb_SearchPatient.Enabled = true;
                    tsb_MedicalHistory.Enabled = true;
                    tsb_PatientPayment.Enabled = true;
                    tsb_ReportRecepPatient.Enabled = true;
                    tsb_StatisticPatient.Enabled = true;
                    tsb_Papers.Enabled = true;
                    break;
            }
        }

        private void ShowAddPatient(HM_ReceptionAndManagementPatient option)
        {
            DisableControl(option);
            AddPatient ap = new AddPatient(HM_ReceptionAndManagementPatient.Patient_Add, 0);
            ap.MdiParent = this;
            ap.Show();
        }

        public void Clear()
        {
            if (ActiveMdiChild != null)
            {
                foreach (Form form in this.MdiChildren)
                    form.Close();
            }
        }

        private void ShowAccount(HR_Employee hR_Employee)
        {
            HR_Employee hr_emp = new HR_Employee();
            hr_emp = BL_HR_Employee.Employee(hR_Employee);
            D_OrganizationDetails d_OrgDetails = new D_OrganizationDetails();
            d_OrgDetails.OrganizationDetailsID = hr_emp.OrganizationDetailsID;
            d_OrgDetails = BL_D_OrganizationDetailss.ObjOrganizationDetails(d_OrgDetails);
            D_Positions dPosition = new D_Positions();
            dPosition.PositionsID = hr_emp.PositionsID;
            dPosition = BL_D_Positions.dPositions(dPosition);
            HR_TypeEmployee hR_TypeEmployee = new HR_TypeEmployee();
            hR_TypeEmployee.TypeEmployeeID = hr_emp.TypeEmployeesID;
            hR_TypeEmployee = BL_HR_TypeEmployee.hrTypeEmpoyee(hR_TypeEmployee);
            HR_TypeDoctor hR_TypeDoc = new HR_TypeDoctor();
            hR_TypeDoc.TypeDoctorID = hR_TypeEmployee.TypeDoctorID;
            hR_TypeDoc = BL_HR_TypeDoctor.hrTypeDoc(hR_TypeDoc);
            Account acc;
            if(log.Role < 8)
                acc = new Account(hr_emp, d_OrgDetails, dPosition,hR_TypeEmployee,hR_TypeDoc,0);
            else
                acc = new Account(hr_emp, d_OrgDetails, dPosition,hR_TypeEmployee,hR_TypeDoc,8);
            acc.MdiParent = this;
            acc.Show();
        }

        public void ShowPatient(HM_ReceptionAndManagementPatient option)
        {
            Clear();
            DisableControl(option);
            ListPatient lp = new ListPatient(option);
            lp.MdiParent = this;
            lp.Show();
        }

        public void EditPatient(int patientID)
        {
            Clear();
            AddPatient ap = new AddPatient(HM_ReceptionAndManagementPatient.Patient_Edit, patientID);
            ap.MdiParent = this;
            ap.Show();
        }

        public void ShowPatientHistory(HM_ReceptionAndManagementPatient option)
        {
            Clear();
            DisableControl(option);
            PatientHistory pa_PatientHistory = new PatientHistory();
            pa_PatientHistory.MdiParent = this;
            pa_PatientHistory.Show();
        }

        public void ShowHistoryOrPayment(HM_ReceptionAndManagementPatient option, Pa_Patient patient)
        {
            DisableControl(option);
            Pa_Patient pa_Patient = new Pa_Patient();
            pa_Patient = BL_Pa_Patients.Patient(patient);
            Clear();
            if (option == HM_ReceptionAndManagementPatient.Patient_MedicalHistory)
            {
                Pa_DetailsMedical pa_Details = BL_Pa_DetailsMedical.Pa_DetailsMedical(pa_Patient);
                MedicalHistory mh = new MedicalHistory(pa_Details, pa_Patient);
                mh.MdiParent = this;
                mh.Show();
            }
            else if (option == HM_ReceptionAndManagementPatient.Patient_Payment)
            {
                PatientPayment pp = new PatientPayment();
                pp.MdiParent = this;
                pp.Show();
            }
        }

        private void DisableManagement()
        {
            (this.tp_ReceptionAndManagementPatient as Control).Enabled = false;
            (this.tp_OpdAndEPrescription as Control).Enabled = false;
            (this.tp_ManagementDR as Control).Enabled = false;
            (this.tp_ManageDrug as Control).Enabled = false;
            (this.tp_Equipment as Control).Enabled = false;
            (this.tp_Dispatch as Control).Enabled = false;
            (this.tp_Library as Control).Enabled = false;
            tssb_System.Enabled = false;
        }

        private void EnableManagement()
        {
            tssb_System.Enabled = true;
            tsmi_Backup.Enabled = false;
            tsmi_Import.Enabled = false;
            tsmi_SettingDatabase.Enabled = false;
            tsmi_SettingSystem.Enabled = false;
            tsmi_Update.Enabled = false;
            if (log == null)
                Application.ExitThread();
            else
            {
                HM_Role  Role = BL_HR_Login.HR_Role(log);
                if (Role == HM_Role.ReceptionAndManagementPatient)
                {
                    (this.tp_ReceptionAndManagementPatient as Control).Enabled = true; tcl_Departments.SelectedIndex = 0;
                        ShowAddPatient(0);
                }
                else if (Role == HM_Role.OpdAndEPrescription)
                {
                    (this.tp_OpdAndEPrescription as Control).Enabled = true; tcl_Departments.SelectedIndex = 1;
                }
                else if (Role == HM_Role.ManagementHumanResources)
                {
                    (this.tp_ManagementDR as Control).Enabled = true;tcl_Departments.SelectedIndex = 2;
                }
                else if (Role == HM_Role.ManagementDrug)
                {
                    (this.tp_ManageDrug as Control).Enabled = true;tcl_Departments.SelectedIndex = 3;
                }
                else if (Role == HM_Role.ManagementEquipment)
                {
                    (this.tp_Equipment as Control).Enabled = true;tcl_Departments.SelectedIndex = 4;
                }
                else if (Role == HM_Role.ManagementDispatch)
                {
                    (this.tp_Dispatch as Control).Enabled = true;tcl_Departments.SelectedIndex = 5;
                }
                else if (Role == HM_Role.ManagementLibrary)
                {
                    (this.tp_Library as Control).Enabled = true; tcl_Departments.SelectedIndex = 6;
                }
                else if (Role == HM_Role.Administrator)
                {
                    EnableAddministrator(); tcl_Departments.SelectedIndex = 0;
                    tsmi_Backup.Enabled = true;
                    tsmi_Import.Enabled = true;
                    tsmi_SettingDatabase.Enabled = true;
                    tsmi_SettingSystem.Enabled = true;
                    tsmi_Update.Enabled = true;
                }
            }
        }

        private void EnableAddministrator()
        {
            (this.tp_ReceptionAndManagementPatient as Control).Enabled = true;
            (this.tp_OpdAndEPrescription as Control).Enabled = true;
            (this.tp_ManagementDR as Control).Enabled = true;
            (this.tp_ManageDrug as Control).Enabled = true;
            (this.tp_Equipment as Control).Enabled = true;
            (this.tp_Dispatch as Control).Enabled = true;
            (this.tp_Library as Control).Enabled = true;
            tssb_System.Enabled = true;
        }

        private void timer_Clock_Tick(object sender, EventArgs e)
        {
            Second += 1;
            if (Second == 60)
            {
                Second = 0;
                Minutes += 1;
            }
            if (Minutes == 60)
                Hours += 1;
            if (Hours == 24)
                Hours = 0;
            if (Second < 10)
                bank1 = "0";
            else
                bank1 = "";
            if (Minutes < 10)
                bank2 = "0";
            else bank2 = "";
            if (Hours < 10)
                bank3 = "0";
            else bank3 = "";
            tssl_ValueTime.Text = bank3 + Hours.ToString() + ":" + bank2 + Minutes.ToString() + ":"+ bank1 + Second.ToString();
        }

        public void Clock()
        {
            Hours = DateTime.Now.Hour;
            Minutes = DateTime.Now.Minute;
            Second = DateTime.Now.Second;
            timer_Clock.Start();
        }

        public void LoadMedicalHistoryDetails(HM_TypeMedicalHistory Type)
        {
            Clear();
            if (Type == HM_TypeMedicalHistory.Bệnh_án_Nội_khoa)
            {
                TypeMedical1 tm1 = new TypeMedical1();
                tm1.MdiParent = this;
                tm1.Show();
            }
            else if (Type == HM_TypeMedicalHistory.Bệnh_án_Nhi_khoa)
            {
                TypeMedical2 tm2 = new TypeMedical2();
                tm2.MdiParent = this;
                tm2.Show();
            }
            else if (Type == HM_TypeMedicalHistory.Bệnh_án_Truyền_nhiễm)
            {
                TypeMedical3 tm3 = new TypeMedical3();
                tm3.MdiParent = this;
                tm3.Show();
            }
            else if (Type == HM_TypeMedicalHistory.Bệnh_án_Phụ_khoa)
            {
                TypeMedical4 tm4 = new TypeMedical4();
                tm4.MdiParent = this;
                tm4.Show();
            }
            else if (Type == HM_TypeMedicalHistory.Bệnh_án_Sản_khoa)
            {
                TypeMedical5 tm5 = new TypeMedical5();
                tm5.MdiParent = this;
                tm5.Show();
            }
        }

        #endregion

        private void tsmi_Exit_Click(object sender, EventArgs e)
        {
            LibraryLanguages.Exit();
        }

        private void tsb_AddDrug_Click(object sender, EventArgs e)
        {
            Clear();
            Add_DetailsDrug AddDrug = new Add_DetailsDrug();
            AddDrug.MdiParent = this;
            AddDrug.Show();
        }

        private void tsb_EditDrug_Click(object sender, EventArgs e)
        {
            Clear();
            Edit_Drug ed = new Edit_Drug();
            ed.MdiParent = this;
            ed.Show();
        }
    }
}
